package com.wyj.utils

import android.content.Context

object ResourceManager {
    private const val R_LAYOUT = "layout"
    private const val R_DRAWABLE = "drawable"
    private const val R_ID = "id"
    private const val R_ANIM = "anim"
    private const val R_RAW = "raw"
    private const val R_STRING = "string"
    private const val R_ATTR = "attr"
    private const val R_ARRAY = "array"
    private const val R_XML = "xml"
    private const val R_MENU = "menu"

    /**
     * 以反射方式获取R.layout资源ID
     *
     * @param packageName
     * @param name
     * @return
     */
    @JvmStatic
    fun getLayoutResId(context: Context, name: String): Int {
        return getResourceIdByName(context, R_LAYOUT, name)
    }

    /**
     * 以反射方式获取R.id资源ID
     *
     * @param packageName
     * @param name
     * @return
     */
    @JvmStatic
    fun getIdResId(context: Context, name: String): Int {
        return getResourceIdByName(context, R_ID, name)
    }

    /**
     * 以反射方式获取R.drawable资源ID
     *
     * @param packageName
     * @param name
     * @return
     */
    @JvmStatic
    fun getDrawableResId(context: Context, name: String): Int {
        return getResourceIdByName(context, R_DRAWABLE, name)
    }

    /**
     * 以反射方式获取R.anim资源ID
     *
     * @param packageName
     * @param name
     * @return
     */
    @JvmStatic
    fun getAnimResId(context: Context, name: String): Int {
        return getResourceIdByName(context, R_ANIM, name)
    }

    /**
     * 以反射方式获取R.raw资源ID
     *
     * @param packageName
     * @param name
     * @return
     */
    @JvmStatic
    fun getRawResId(context: Context, name: String): Int {
        return getResourceIdByName(context, R_RAW, name)
    }

    /**
     * 以反射方式获取R.string资源ID
     *
     * @param packageName
     * @param name
     * @return
     */
    @JvmStatic
    fun getStringResId(context: Context, name: String): Int {
        return getResourceIdByName(context, R_STRING, name)
    }

    /**
     * 以反射方式获取R.attr资源ID
     *
     * @param packageName
     * @param name
     * @return
     */
    @JvmStatic
    fun getAttrResId(context: Context, name: String): Int {
        return getResourceIdByName(context, R_ATTR, name)
    }

    /**
     * 以反射方式获取R.array资源ID
     *
     * @param packageName
     * @param name
     * @return
     */
    @JvmStatic
    fun getArrayResId(context: Context, name: String): Int {
        return getResourceIdByName(context, R_ARRAY, name)
    }

    /**
     * 以反射方式获取R.xml资源ID
     *
     * @param packageName
     * @param name
     * @return
     */
    @JvmStatic
    fun getXmlResId(context: Context, name: String): Int {
        return getResourceIdByName(context, R_XML, name)
    }

    /**
     * 以反射方式获取R.menu资源ID
     *
     * @param packageName
     * @param name
     * @return
     */
    @JvmStatic
    fun getMenuResId(context: Context, name: String): Int {
        return getResourceIdByName(context, R_MENU, name)
    }

    /**
     * http://stackoverflow.com/questions/14373004/java-lang-noclassdeffounderror-com-facebook-android-rlayout-error-when-using-f
     *
     * @param packageName
     * @param className
     * @param name
     * @return
     */
    @JvmStatic
    private fun getResourceIdByName(context: Context, defType: String, name: String): Int {
        return context.resources.getIdentifier(name, defType, context.packageName)
    }
}